package com.swpu.project.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 通用返回类
 *
 */

@Data
@NoArgsConstructor
public class BaseResponse<T> implements Serializable {

    private int code;

    private T data;

    private String message;

    private  boolean isSuccessful;




    public  static BaseResponse ok(Object data){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(201);
        baseResponse.setData(data);
        baseResponse.setSuccessful(true);
        return baseResponse;
    }

    public  static BaseResponse fail(String message,int code){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(code);
        baseResponse.setMessage(message);
        baseResponse.setSuccessful(false);
        return baseResponse;
    }
    public  static BaseResponse fail(String message){
        BaseResponse baseResponse = new BaseResponse();

        baseResponse.setMessage(message);
        baseResponse.setSuccessful(false);
        return baseResponse;
    }
    public  static BaseResponse ok(){
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(201);
        baseResponse.setMessage("请求成功");
        baseResponse.setSuccessful(true);
        return baseResponse;
    }
    public  static BaseResponse iFSuccess(Boolean judge)
    {
        BaseResponse<Object> objectBaseResponse = new BaseResponse<>();
        objectBaseResponse.setSuccessful(judge);
return objectBaseResponse;
    }
    public BaseResponse(int code, String message,T data) {
        this.code = code;
        this.data = data;
        this.message = message;
    }

    public BaseResponse(int code, T data) {
        this(code, "",data);
    }

    public BaseResponse(ErrorCode errorCode) {
        this(errorCode.getCode(), null,null);
    }
}
